﻿using Zore.App.Shared.Helpers;

namespace App.Core.Result
{
    /// <summary>
    /// 统一响应结果泛型
    /// </summary>
    /// <typeparam name="T">返回结果的数据类型</typeparam>
    public class UnifyResult<T> : UnifyResult
    {
        public UnifyResult()
        {

        }

        public UnifyResult(T result, ResultCode code = ResultCode.Success) : base(code)
        {
            Result = result;
        }
        /// <summary>
        /// 数据
        /// </summary>
        public T Result { get; set; }

        /// <summary>
        /// 正常消息并返回数据
        /// </summary>
        /// <param name="result"></param>
        public static implicit operator UnifyResult<T>(T result)
        {
            return new UnifyResult<T>
            {
                Result = result,
                StatusCode = ResultCode.Success,
                Message = ResultCode.Success.GetDescription()
            };
        }

        /// <summary>
        /// 异常消息
        /// </summary>
        /// <param name="exception"></param>
        public static implicit operator UnifyResult<T>(Exception exception)
        {
            return new UnifyResult<T>
            {
                Message = exception.Message,
                StatusCode = ResultCode.Error
            };
        }
    }
}
